//
//  BoundConnectingView.swift
//  EasePods
//
//  Created by 李然 on 2024/5/6.
//

import UIKit
import Lottie

class BoundConnectingView: View {
    
    override func makeUI() {
        super.makeUI()
        
        backgroundColor = R.color.common_2_bg_page()
        
        addSubview(animationView)
        animationView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.size.equalTo(CGSize(width: kScreenWidth, height: kScreenHeight))
        }
        
        addSubview(deviceImageView)
        deviceImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        addSubview(statueAnimationView)
        statueAnimationView.snp.makeConstraints { make in
            make.top.equalTo(deviceImageView)
            make.trailing.equalTo(deviceImageView).offset(-20)
            make.size.equalTo(CGSize(width: 42, height: 42))
        }
        
        addSubview(connectingLabel)
        connectingLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(deviceImageView.snp.bottom).offset(114)
        }
        
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.width.equalTo(kScreenWidth - 32)
            make.centerX.equalToSuperview()
            make.top.equalTo(deviceImageView.snp.bottom).offset(26)
        }
        
        addSubview(failedRetryLabel)
        failedRetryLabel.snp.makeConstraints { make in
            make.width.centerX.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
        }
        
        addSubview(retryBtn)
        retryBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(failedRetryLabel.snp.bottom).offset(66)
            make.size.equalTo(CGSize(width: 154, height: 48))
        }
    }
    
    func setStatus(_ status: BoundConnectStatus, complete:(()->Void)? = nil) {
        
        self.animationView.isHidden = true
        self.connectingLabel.isHidden = true
        self.statueAnimationView.isHidden = true
        self.titleLabel.isHidden = true
        self.failedRetryLabel.isHidden = true
        self.retryBtn.isHidden = true
        
        switch status {
        case .connecting:
            self.animationView.isHidden = false
            self.connectingLabel.isHidden = false
        case .connected:
            self.titleLabel.isHidden = false
            self.titleLabel.text = R.string.localizable.bound_device_added_tip()
            self.statueAnimationView.isHidden = false
            self.statueAnimationView.animation = LottieAnimation.named("ani_bound_connect_success")
            self.statueAnimationView.play { completed in
                complete?()
            }
        case .connectFailed:
            self.titleLabel.isHidden = false
            self.titleLabel.text = R.string.localizable.bound_device_add_failed()
            self.failedRetryLabel.isHidden = false
            self.retryBtn.isHidden = false
            self.statueAnimationView.isHidden = false
            self.statueAnimationView.animation = LottieAnimation.named("ani_bound_connect_fail")
            self.statueAnimationView.play()
        }
    }

    lazy var deviceImageView: UIImageView = {
        let view = UIImageView()
        view.image = R.image.img_common_earcase()
        return view
    }()

    lazy var animationView: LottieAnimationView = {
        let view = LottieAnimationView()
        let animation = LottieAnimation.named("ani_bound_connecting")
        view.animation = animation
        view.loopMode = .loop
        view.backgroundBehavior = .pauseAndRestore
        view.contentMode = .scaleAspectFill
        view.play()
        return view
    }()

    lazy var connectingLabel: UILabel = {
        let label = UILabel()
        label.textColor = R.color.common_3_text()
        label.font = Fonts.systemFont(ofSize: 14)
        label.numberOfLines = 0
        label.text = R.string.localizable.bound_connecting()
        label.textAlignment = .center
        return label
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = R.color.common_3_text()
        label.font = Fonts.systemFont(ofSize: 20)
        label.numberOfLines = 0
        label.textAlignment = .center
        return label
    }()
    
    lazy var failedRetryLabel: UILabel = {
        let label = UILabel()
        label.textColor = R.color.common_23_text_second()
        label.font = Fonts.systemFont(ofSize: 14)
        label.numberOfLines = 0
        label.textAlignment = .center
        return label
    }()
    
    lazy var retryBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.layer.cornerRadius = 6
        btn.backgroundColor = R.color.common_22_button_n()
        btn.setTitle(R.string.localizable.common_retry(), for: .normal)
        btn.titleLabel?.font = Fonts.systemFont(ofSize: 16,weight: .semibold)
        btn.setTitleColor(R.color.common_24_button_text(), for: .normal)
        return btn
    }()
    
    lazy var statueAnimationView: LottieAnimationView = {
        let view = LottieAnimationView()
        view.loopMode = .playOnce
        view.backgroundBehavior = .pauseAndRestore
        return view
    }()
}
